$(function(){
    if (!String.prototype.trim) {
        String.prototype.trim = function () {
            return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
        };
    }
    $('#findPass').validate({
        rules: {
            phone: {
                required: true,
                rangelength: [11,11],
                checkPhone: true
            },
            yzm: {
                required: true,
                rangelength: [4,4]
            },
            pass: {
                required: true,
                rangelength: [8,20]
            }
        },
        messages: {
            phone: {
                required: '手机号码不能为空',
                rangelength: '请输入正确的手机号码'
            },
            yzm: {
                required: '验证码不能为空',
                rangelength: '请输入正确的验证码'
            },
            pass: {
                required: '密码不能为空',
                rangelength: '密码长度为8-20位'
            }
        },
        debug: true,
        focusInvalid: false,
        onkeyup: false,
        success: 'valid',
        submitHandler:function(){
            $('#findPass').find(":submit").attr("disabled", true);
            var phone = $('#phone').val().trim();
            var sverifyCode = $('#yzm').val().trim();
            var password = $('#pass').val().trim();
            $.ajax({
                url: '/findPassword',
                data: {
                    phone: phone,
                    sverifyCode: sverifyCode,
                    password: password
                },
                type: 'post',
                dateType: 'json',
                success: function(data){
                    if(data.success){
                        window.location.href='/login'
                    }else{
                        $.errorHandler('#findPass','',data.msg);
                    }
                    $('#findPass').find(":submit").attr("disabled", false);
                },
                error: function(){}
            })
        }
    });

    //倒计时
    var timer;
    function count(time){
        time--;
        if(time>=0){
            timer = setTimeout(function(){
                count(time);
            },1000);
            $('.getMess').html(time+'s');
        }else{
            clearTimeout(timer);
            $('.getMess').html('重新发送验证码').removeClass('disabled');
        }
    }
    $('.getMess').on('click',function(){
        var phoneNum = $('#phone').val().trim();
        if(phoneNum == ''){
            $.errorHandler('#findPass','','请输入手机号');
        }else{
            var flag = $(this).hasClass('disabled');
            if(flag){
                return false;
            }else{
                $(this).addClass('disabled');
                count(60);
                $.ajax({
                    url: '/sendPass',
                    data: {
                        phone: phoneNum
                    },
                    type: 'get',
                    dateType: 'json',
                    success:function(data){
                        if(!data.success){
                            $.errorHandler('#findPass','',data.msg);
                        }
                    },
                    error:function(){}
                });
            }
        }
    });
});